const { getlovedata, findproduct, dlelovesdata, setlovesdata } = require('../service/loves')
module.exports = {
    async getloves(ctx, next) {
        const { userid, limit, offset } = ctx.query
        const ruata = await getlovedata({ userid: +userid })
        if (ruata.length == 0) {
            return ctx.body = {
                code: 200,
                msg: '没有喜欢',
                data: []
            }
        }
        const prodid = ruata.map(item => item.productid)
        console.log(prodid)
        const restdata = await findproduct(prodid, limit, offset)
        return ctx.body = {
            code: 200,
            msg: '获取成功',
            data: restdata
        }
    },
    async setloves(ctx, next) {
        const { userid, productid } = ctx.query
        const rudata = await setlovesdata({ userid, productid })
        return ctx.body = {
            code: 200,
            msg: '添加成功',
        }
    },
    async dleloves(ctx, next) {
        const { userid, productid } = ctx.query
        const rudata = await dlelovesdata({ userid, productid })
        return ctx.body = {
            code: 200,
            msg: '删除成功',
        }
    },
    async isloves(ctx, next) {
        const { userid, productid } = ctx.query
        const ruata = await getlovedata({ userid, productid })
        if (ruata.length > 0) {
            return ctx.body = {
                code: 200,
                msg: '已收藏',
                data: true
            }
        } else {
            return ctx.body = {
                code: 200,
                msg: '未收藏',
                data: false

            }
        }
    }
}